<?php
    session_start();
    function xmlentities($xml)
    {
        $entities = array ('<' => '&lt;','&' => '&amp;',);
        if (is_string($xml)) { $xml = strtr($xml, $entities);};
        return $xml;
    }
    if (isset($_POST['alt']))
    {
        $arq_xml=DOMDocument::load('./user/'.$_SESSION['id_usuario'].'/biblioteca.xml');
        $x = $arq_xml->getelementsbytagname($_POST['categoria']);
        $i = $_POST['exemplo'];
        $desc = $x->item($i)->getelementsbytagname('description');
        $come = $x->item($i)->getelementsbytagname('comments');
        $refe = $x->item($i)->getelementsbytagname('reference');
        if (isset($_POST['desc'])) $desc->item(0)->nodeValue=xmlentities(utf8_encode($_POST['desc']));
        if (isset($_POST['comment'])) $come->item(0)->nodeValue=xmlentities(utf8_encode($_POST['comment']));
        if (isset($_POST['ref'])) $refe->item(0)->nodeValue=xmlentities(utf8_encode($_POST['ref']));
        switch ($_POST['categoria'])
        {
            case 'floatp':
                if ($x->length > $i)
                {
                    $base = $x->item($i)->getelementsbytagname('base');
                    $mant = $x->item($i)->getelementsbytagname('mantissa');
                    $expo = $x->item($i)->getelementsbytagname('exponent');
                    $lowe = $expo->item(0)->getelementsbytagname('lower');
                    $uppe = $expo->item(0)->getelementsbytagname('upper');
                    $deci = $x->item($i)->getelementsbytagname('decimal');
                    if ($deci->length) $xis = $deci->item(0)->getelementsbytagname('x');
                    $base->item(0)->nodeValue=$_POST['base'];
                    $mant->item(0)->nodeValue=$_POST['mantissa'];
                    $lowe->item(0)->nodeValue=$_POST['expoenteinf'];
                    $uppe->item(0)->nodeValue=$_POST['expoentesup'];
                    if (isset($_POST['decimal1']))
                    {
                        $xis->item(0)->nodeValue=$_POST['decimal1'];
                        $xis->item(1)->nodeValue=$_POST['decimal2'];
                    }
                }
                break;
            case 'matrix':
                $dim = $x->item($i)->getelementsbytagname('dim');
                $numrows = $x->item($i)->getelementsbytagname('num_rows');
                $numcols = $x->item($i)->getelementsbytagname('num_cols');
                if (isset($_POST['dim'])) // determinante, inversao, definida positiva, decomposicao de matrizes
                {
                    $dim->item(0)->nodeValue = $_POST['dim'];
                    $rows = $x->item($i)->getelementsbytagname('row');
                    for ($j=0; $j<$rows->length; $j++)
                    {
                        $cols = $rows->item($j)->getelementsbytagname('x');
                        for ($k=0; $k<$cols->length; $k++)
                        {
                            $cols->item($k)->nodeValue = $_POST['A'][$j][$k];
                        }
                    }
                }
                else
                {
                    if (isset($_POST['num_cols'])) //multiplicacao de matrizes
                    {
                        $numrows->item(0)->nodeValue=$_POST['num_rows'];
                        $numcols->item(0)->nodeValue=$_POST['num_cols'];
                        $rows = $x->item($i)->getelementsbytagname('row');
                        for ($j=0; $j<$numrows->item(0)->nodeValue; $j++)
                        {
                            $cols = $rows->item($j)->getelementsbytagname('x');
                            for ($k=0; $k<$numcols->item(0)->nodeValue; $k++)
                            {
                                $cols->item($k)->nodeValue=$_POST['A'][$j][$k];
                            }
                        }
                        for ($j=$numrows->item(0)->nodeValue; $j<($numrows->item(0)->nodeValue*2); $j++)
                        {
                            $cols = $rows->item($j)->getelementsbytagname('x');
                            for ($k=0; $k<$numcols->item(0)->nodeValue; $k++)
                            {
                                $cols->item($k)->nodeValue=$_POST['B'][($j-$numrows->item(0)->nodeValue)][$k];
                            }
                        }
                    }
                    else //soma de matrizes
                    {
                        $numrows->item(0)->nodeValue=$_POST['num_rows1'];
                        $numcols->item(0)->nodeValue=$_POST['num_cols1'];
                        $numrows->item(1)->nodeValue=$_POST['num_rows2'];
                        $numcols->item(1)->nodeValue=$_POST['num_cols2'];
                        $rows = $x->item($i)->getelementsbytagname('row');
                        for ($j=0; $j<$numrows->item(0)->nodeValue; $j++)
                        {
                            $cols = $rows->item($j)->getelementsbytagname('x');
                            for ($k=0; $k<$numcols->item(0)->nodeValue; $k++)
                            {
                                $cols->item($k)->nodeValue=$_POST['A'][$j][$k];
                            }
                        }
                        for ($j=$numrows->item(0)->nodeValue; $j<($numrows->item(0)->nodeValue+$numrows->item(1)->nodeValue); $j++)
                        {
                            $cols = $rows->item($j)->getelementsbytagname('x');
                            for ($k=0; $k<$numcols->item(1)->nodeValue; $k++)
                            {
                                $cols->item($k)->nodeValue=$_POST['B'][($j-$numrows->item(0)->nodeValue)][$k];
                            }
                        }
                    }
                }
                    break;
                case 'system':
                    $meto = $x->item($i)->getelementsbytagname('method');
                    $rows = $x->item($i)->getelementsbytagname('row');
                    $prop = $x->item($i)->getelementsbytagname('properties');
                    $dime = $x->item($i)->getelementsbytagname('dim');
                    if ($meto->length && $meto->item(0)->nodeValue == 'iterative')
                    {
                        $maxi = $x->item($i)->getelementsbytagname('maxiter');
                        $epsi = $x->item($i)->getelementsbytagname('epsilon');
                        $init = $x->item($i)->getelementsbytagname('initial_vector');
                        $vini = $init->item(0)->getelementsbytagname('x');
                    }
                    $cols = array();
                    for ($j=0; $j<$rows->length; $j++)
                    {
                        $cols[$j] = $rows->item($j)->getelementsbytagname('x');
                    }
                    for ($j=0; $j<$prop->length; $j++)
                    {
                        if ($prop->item($j)->hasAttribute('type_solution')) $prop->item($j)->setAttribute('type_solution',(($_POST['tiposolucao'] == 1)?'compatible_determined':(($_POST['tiposolucao'] == 2)?'compatible_undetermined':'unsolvable')));
                        if ($prop->item($j)->hasAttribute('symetric')) $prop->item($j)->setAttribute('symetric',((isset($_POST['symetric']))?'symetric':'nonsymetric'));
                        if ($prop->item($j)->hasAttribute('sparsity')) $prop->item($j)->setAttribute('sparsity',((isset($_POST['sparsity']))?'sparse':'nonsparse'));
                        if ($prop->item($j)->hasAttribute('positive_definite')) $prop->item($j)->setAttribute('positive_definite',((isset($_POST['defpos']))?'positive_definite':'nonpositive_definite'));
                        if ($prop->item($j)->hasAttribute('diagonally_dominant')) $prop->item($j)->setAttribute('diagonally_dominant',((isset($_POST['diagdominante']))?'diagonally_dominant':'nondiagonally_dominant'));
                    }
                    if (isset($maxi) && $maxi->length) $maxi->item(0)->nodeValue=$_POST['maxiter'];
                    if (isset($epsi) && $epsi->length) $epsi->item(0)->nodeValue=$_POST['erro'];
                    for ($j=0; $j<$rows->length; $j++)
                    {
                        if ($cols[$j]->length)
                        {
                            for ($k=0; $k<$cols[$j]->length; $k++)
                            {
                                $cols[$j]->item($k)->nodeValue=$_POST['A'][$j][$k];
                            }
                        }
                    }
                    if (isset($init))
                    {
                        for ($j=0; $j<$vini->length; $j++)
                        {
                            $vini->item($j)->nodeValue=$_POST['vinicial'][$k];
                        }
                    }
                    break;
                case 'interpol':
                    $opt = $x->item($i)->getelementsbytagname('options');
                    $nump = $x->item($i)->getelementsbytagname('num_points');
                    $valor = $x->item($i)->getelementsbytagname('interpol_value');
                    if ($opt->length > 1)
                    {
                        $dist = $opt->item(1)->getAttribute('distribution');
                        $func = $x->item($i)->getelementsbytagname('function');
                        $int = $x->item($i)->getelementsbytagname('interval');
                        $int1 = $int->item(0)->getelementsbytagname('x');
                        $int2 = $int->item(0)->getelementsbytagname('y');
                        $nump->item(0)->nodeValue=$_POST['numpoint'];
                        $opt->item(1)->setAttribute('distribution',$_POST['dist']);
                        $func->item(0)->nodeValue=xmlentities(utf8_encode($_POST['funcao']));
                        $int1->item(0)->nodeValue=$_POST['int1'];
                        $int2->item(0)->nodeValue=$_POST['int2'];
                    }
                    elseif ($opt->length == 1)
                    {
                        $ponto = $x->item($i)->getelementsbytagname('point');
                        for ($j=0; $j<$ponto->length; $j++)
                        {
                            $ponto->item($j)->getelementsbytagname('x')->item(0)->nodeValue=$_POST['pontox'][$j];
                            $ponto->item($j)->getelementsbytagname('y')->item(0)->nodeValue=$_POST['pontoy'][$j];
                        }
                    }
                    $valor->item(0)->nodeValue=$_POST['intvalue'];
                    break;
                case 'datafit':
                    $opt = $x->item($i)->getelementsbytagname('options');
                    $dados = $opt->item(0)->getAttribute('data');
                    $grau = $x->item($i)->getelementsbytagname('degree');
                    switch ($dados)
                    {
                        case 'discrete':
                            $nump = $x->item($i)->getelementsbytagname('num_points');
                            $points = $x->item($i)->getelementsbytagname('point');
                            for ($j=0; $j<$points->length; $j++)
                            {
                                $points->item($j)->getelementsbytagname('x')->item(0)->nodeValue=$_POST['pontox'][$j];
                                $points->item($j)->getelementsbytagname('y')->item(0)->nodeValue=$_POST['pontoy'][$j];
                            }
                            break;
                        case 'continuous':
                            $funcao = $x->item($i)->getelementsbytagname('function');
                            $interv = $x->item($i)->getelementsbytagname('interval');
                            $int1 = $interv->item(0)->getelementsbytagname('x');
                            $int2 = $interv->item(0)->getelementsbytagname('y');
                            $funcao->item(0)->nodeValue=$_POST['funcao'];
                            $int1->item(0)->nodeValue=$_POST['int1'];
                            $int2->item(0)->nodeValue=$_POST['int2'];
                            break;
                    }
                    $grau->item(0)->nodeValue=$_POST['degree'];
                    break;
                case 'equation':
                    $opt = $x->item($i)->getelementsbytagname('option');
                    $tipo = $opt->item(0)->getAttribute('data');
                    $maxiter = $x->item($i)->getelementsbytagname('maxiter');
                    $erro = $x->item($i)->getelementsbytagname('error');
                    $grau = $x->item($i)->getelementsbytagname('degree');
                    if ($grau->length)
                        for ($j=0; $j<=$grau->item(0)->nodeValue; $j++)
                            $x->item($i)->getelementsbytagname('coef')->item($j)->nodeValue=$_POST['coef'][$j];
                    $function = $x->item($i)->getelementsbytagname('function');
                    $interv = $x->item($i)->getelementsbytagname('interval');
                    if ($interv->length)
                    {
                        $interv->item(0)->getelementsbytagname('x')->item(0)->nodeValue=$_POST['int1'];
                        $interv->item(0)->getelementsbytagname('y')->item(0)->nodeValue=$_POST['int2'];
                    }
                    $initv = $x->item($i)->getelementsbytagname('initial_vector');
                    if ($initv->length) $vinic = $initv->item(0)->getelementsbytagname('x');
                    if ($function->length)
                    {
                        $function->item(0)->nodeValue=$_POST['function'];
                    }
                    $maxiter->item(0)->nodeValue=$_POST['mxiter'];
                    $erro->item(0)->nodeValue=$_POST['error'];
                    if ($initv->length)
                        for ($j=0; $j<$vinic->length; $j++)
                            $vinic->item($j)->nodeValue=$_POST['vinic'][$j];
                    break;
                case 'nl_system':
                    $dim = $x->item($i)->getelementsbytagname('dim');
                    $maxiter = $x->item($i)->getelementsbytagname('maxiter');
                    $erro = $x->item($i)->getelementsbytagname('epsilon');
                    $linha = $x->item($i)->getelementsbytagname('nl_row');
                    $maxiter->item(0)->nodeValue=$_POST['mxiter'];
                    $erro->item(0)->nodeValue=$_POST['erro'];
                    for ($j=0; $j<$linha->length; $j++)
                    {
                        $coef = $linha->item($j)->getelementsbytagname('x');
                        $exp = $linha->item($j)->getelementsbytagname('exp');
                        $k=0;
                        foreach($exp as $ex)
                        {
                            $coef->item($k)->nodeValue=$_POST['coef'][$j][$k];
                            $ex->nodeValue=$_POST['exp'][$j][$k++];
                        }
                        $coef->item($coef->length-1)->nodeValue=$_POST['res'][$j];
                    }
                    $vinic = $x->item($i)->getelementsbytagname('initial_vector');
                    $vx = $vinic->item(0)->getelementsbytagname('x');
                    for ($j=0; $j<$vx->length;$j++)
                        $vx->item($j)->nodeValue=$_POST['vinic'][$j];
                    $prop = $x->item($i)->getelementsbytagname('properties');
                    for ($j=0; $j<$prop->length; $j++)
                    {
                        if ($prop->item($j)->hasAttribute('type_solution')) $prop->item($j)->setAttribute('type_solution',(($_POST['tiposolucao'] == 1)?'compatible_determined':(($_POST['tiposolucao'] == 2)?'compatible_undetermined':'unsolvable')));
                        if ($prop->item($j)->hasAttribute('symetric')) $prop->item($j)->setAttribute('symetric',((isset($_POST['symetric']))?'symetric':'nonsymetric'));
                        if ($prop->item($j)->hasAttribute('sparsity')) $prop->item($j)->setAttribute('sparsity',((isset($_POST['sparsity']))?'sparse':'nonsparse'));
                        if ($prop->item($j)->hasAttribute('positive_definite')) $prop->item($j)->setAttribute('positive_definite',((isset($_POST['defpos']))?'positive_definite':'nonpositive_definite'));
                        if ($prop->item($j)->hasAttribute('diagonally_dominant')) $prop->item($j)->setAttribute('diagonally_dominant',((isset($_POST['diagdominante']))?'diagonally_dominant':'nondiagonally_dominant'));
                    }
                    break;
                case 'derivate':
                    $func = $x->item($i)->getelementsbytagname('function');
                    $init = $x->item($i)->getelementsbytagname('initial_value');
                    $iter = $x->item($i)->getelementsbytagname('maxiter');
                    $erro = $x->item($i)->getelementsbytagname('error');
                    $func->item(0)->nodeValue=$_POST['funcao'];
                    $init->item(0)->nodeValue=$_POST['vinicial'];
                    $iter->item(0)->nodeValue=$_POST['maxiter'];
                    $erro->item(0)->nodeValue=$_POST['erro'];
                    break;
                case 'integrate':
                    $func = $x->item($i)->getelementsbytagname('function');
                    $limit = $x->item($i)->getelementsbytagname('limit');
                    $limit->item(0)->getelementsbytagname('a')->item(0)->nodeValue=$_POST['a'];
                    $limit->item(0)->getelementsbytagname('b')->item(0)->nodeValue=$_POST['b'];
                    $part = $x->item($i)->getelementsbytagname('partitions');
                    $part->item(0)->getelementsbytagname('n')->item(0)->nodeValue=$_POST['n'];
                    $part->item(0)->getelementsbytagname('m')->item(0)->nodeValue=$_POST['m'];
                    $func->item(0)->nodeValue=$_POST['funcao'];
                    break;
            }
        echo "Exemplo alterado com sucesso!";
        $arq_xml->save('./user/'.$_SESSION['id_usuario'].'/biblioteca.xml');
    }
    echo "<br /><br /><a href=\"./remocao.php\">In&iacute;cio</a>";
?>
